package com.test.cordova.util;

/**
 * 加密工具
 */

public class CodeUtil {

    //字符串转byte数组
    public static byte[] StringToBytesArray(String text) {
        if ("".equals(text) || text == null) {
            return null;
        }
        byte[] data = null;
        try {
            data = text.getBytes("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }


    /**
     * Base64 加密
     * @param datas
     * @return
     */
    public static String[] allEncode(String ... datas){
        String[] result = new String[datas.length];
        try {
            for (int i = 0; i < datas.length; i++) {
                //去除空格
                String data = datas[i].replaceAll(" ","");
                // 字符串转为数组
                byte[] dataArray = StringToBytesArray(data);
                // base64
                String dataEncode = Base64.encode(dataArray);
                result[i] = dataEncode;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * Base64解密
     * @param dencodeDatas
     * @return
     */
    public static String[] allDencode(String ... dencodeDatas){
        String[] result = new String[dencodeDatas.length];
        try {
            for (int i = 0; i < dencodeDatas.length; i++) {
                byte[] decode =  Base64.decode(dencodeDatas[i]);
                String resultData = new String(decode, "UTF-8");
                result[i] = resultData;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
